package org.yjhking.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yjhking.domain.MovieTicket;
import org.yjhking.domain.User;
import org.yjhking.dto.Movie;
import org.yjhking.dto.UserTicket;
import org.yjhking.feignclient.UserFeignClient;
import org.yjhking.mapper.MovieTicketMapper;
import org.yjhking.query.AjaxResult;
import org.yjhking.service.MovieTicketService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author YJH
 */
@Service
public class MovieTicketServiceImpl implements MovieTicketService {
    
    @Resource
    private MovieTicketMapper movieTicketMapper;
    @Resource
    private UserFeignClient userFeignClient;
    
    @Override
    public int deleteByPrimaryKey(Long id) {
        return movieTicketMapper.deleteByPrimaryKey(id);
    }
    
    @Override
    public int insert(MovieTicket record) {
        return movieTicketMapper.insert(record);
    }
    
    @Override
    public int insertSelective(MovieTicket record) {
        return movieTicketMapper.insertSelective(record);
    }
    
    @Override
    public MovieTicket selectByPrimaryKey(Long id) {
        return movieTicketMapper.selectByPrimaryKey(id);
    }
    
    @Override
    public int updateByPrimaryKeySelective(MovieTicket record) {
        return movieTicketMapper.updateByPrimaryKeySelective(record);
    }
    
    @Override
    public int updateByPrimaryKey(MovieTicket record) {
        return movieTicketMapper.updateByPrimaryKey(record);
    }
    
    @Override
    public AjaxResult getMovieList() {
        // 从数据库查询所有电影票
        List<MovieTicket> movieTickets = movieTicketMapper.selectAll();
        return new AjaxResult(true, "查询成功", movieTickets);
    }
    
    @Transactional
    @Override
    public AjaxResult buy(Movie movie) {
        // 通过电影票id查询电影票
        MovieTicket movieTicket = movieTicketMapper.selectByPrimaryKey(movie.getMovieId());
        // 判断库存
        if (movieTicket.getStock() <= 0) {
            return new AjaxResult(false, "库存不足");
        }
        // 获取用户对象
        AjaxResult userResult = userFeignClient.getUserById(movie.getUserId());
        // 判断用户是否存在
        if (!userResult.getSuccess()) {
            return new AjaxResult(false, "用户不存在");
        }
        ObjectMapper objectMapper = new ObjectMapper();
        // 将返回的Object对象转换成User对象
        User user = objectMapper.convertValue(userResult.getResultObj(), User.class);
        // 判断用户余额是否足够
        if (user.getBalance() < movieTicket.getPrice().longValue()) {
            return new AjaxResult(false, "余额不足");
        }
        // 扣余额
        user.setBalance(user.getBalance() - movieTicket.getPrice().longValue());
        // 渐库存
        movieTicket.setStock(movieTicket.getStock() - 1);
        // 更新用户余额
        userFeignClient.updateUser(user);
        // 更新电影票库存
        movieTicketMapper.updateByPrimaryKeySelective(movieTicket);
        // 更新中间表
        movieTicketMapper.movieTicket(new UserTicket(user.getId(), movieTicket.getId(), false));
        return new AjaxResult(true, "购买成功");
    }
    
    @Override
    public AjaxResult selectByMovieId(Movie movie) {
        // 从数据库查对应用户的所有电影票信息
        List<Boolean> stock = movieTicketMapper.selectStockByUserIdAndMovieId(movie);
        // 判断电影票是否存在
        if (stock == null) {
            return new AjaxResult(false, "电影票不存在");
        }
        // 设置一个默认值
        boolean b = true;
        // 遍历集合查询电影票是否过期
        for (Boolean aBoolean : stock) {
            if (!aBoolean) {
                b = false;
                break;
            }
        }
        // 如果过期
        if (b) {
            return new AjaxResult(false, "电影票不可观看");
        }
        return new AjaxResult(true, "电影票可观看");
    }
    
    @Transactional
    @Override
    public AjaxResult free(Long id) {
        // 查询所有电影票
        List<MovieTicket> list = movieTicketMapper.selectAll();
        // 获取第一张票
        MovieTicket movieTicket = list.get(0);
        // 更新电影票库存
        movieTicket.setStock(movieTicket.getStock() - 1);
        movieTicketMapper.updateByPrimaryKeySelective(movieTicket);
        // 更新中间表
        movieTicketMapper.movieTicket(new UserTicket(id, movieTicket.getId(), false));
        return new AjaxResult(true, "赠送电影票成功");
    }
    
    @Override
    public AjaxResult watchMovie(Movie movie) {
        // 从数据库查对应用户的所有电影票信息
        List<Boolean> srock = movieTicketMapper.selectStockByUserIdAndMovieId(movie);
        // 判断电影票是否存在
        if (srock == null) {
            return new AjaxResult(false, "电影票不存在");
        }
        // 设置一个默认值
        boolean b = true;
        // 遍历集合查询电影票是否过期
        for (Boolean aBoolean : srock) {
            if (!aBoolean) {
                b = false;
                break;
            }
        }
        // 如果过期
        if (b) {
            return new AjaxResult(false, "电影票已过期或已观看");
        }
        // 通过id查电影票信息
        MovieTicket movieTicket = movieTicketMapper.selectByPrimaryKey(movie.getMovieId());
        // 判断电影放映是否结束
        if (new Date().before(movieTicket.getEndTime())) {
            // 如果结束则使票作废
            movieTicketMapper.setStatus(new UserTicket(movie.getUserId(), movie.getMovieId(), true));
            return new AjaxResult(false, "电影票已过期");
        }
        // 观影成功，并使票作废
        movieTicketMapper.setStatus(new UserTicket(movie.getUserId(), movie.getMovieId(), true));
        return new AjaxResult(true, "观看成功");
    }
}
